package com.xiaoxi.visitormode.composite;

import com.xiaoxi.visitormode.visitor.Visitor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.visitormode
 * @ClassName Directory
 * @Description TODO
 * @Date 2023/3/16 15:53
 * @Version 1.0
 */
public class Directory extends AbstractEntry {

    private String name;
    private List<Entry> directoryList = new ArrayList<>();

    public Directory(String name) {
        this.name = name;
    }

    @Override
    public void add(Entry entry) throws FileNotAddException {
        entry.parent = this;
        this.directoryList.add(entry);
    }

    @Override
    public void printList(String prefix) {
        System.out.println(prefix + "/" + name + " ( 该目录总大小：" + getSize() + " ) ");
        for (Entry entry : directoryList) {
            entry.printList(prefix + "/" + name);
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getSize() {
        int size = 0;
        for (Entry entry : directoryList) {
            size += entry.getSize();
        }
        return size;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }

    public Iterator createIterator() {
        return directoryList.listIterator();
    }
}
